import router from '@/router'
import { start, close } from '@/utils/nprogress'
import useUserStore from '@/store/user'

const whiteList = ['/login']
router.beforeEach(async (to) => {
    start()
    const token = localStorage.getItem('access_token')
    if (whiteList.includes(to.path)) {
        return true
    }
    if (!token) {
        return '/login'
    }
    const userStore = useUserStore()
    if (!userStore.userInfo) {
        await userStore.fetchUserInfo()
        return to.fullPath
    }
})
// 路由后置守卫
router.afterEach(() => {
    close()
})